﻿Imports System.Data.SqlClient
Imports DTO
Public Class HoaDonDAO
    Public Sub CapNhatNhanVienTrucVaoHoaDonDAO(ByVal _hoaDonDTO As HoaDonDTO)
        Dim sql As String = "sp_CapNhatNhanVienTrucVaoHoaDon"

        Dim _listPara As New List(Of SqlParameter)

        Dim MaHD As New SqlParameter("@MaHD", SqlDbType.Int)
        MaHD.Value = _hoaDonDTO.IMaHD
        _listPara.Add(MaHD)

        Dim MaNV As New SqlParameter("@MaNV", SqlDbType.Int)
        MaNV.Value = _hoaDonDTO.IMaNV
        _listPara.Add(MaNV)

        DataProvider.ExcuteQueryProc(sql, _listPara)

    End Sub

    Public Function LayMaHDBanDangSuDungDAO(ByVal _hoaDonDTO As HoaDonDTO) As Integer
        Dim sql As String = "Select MaHD from HoaDon where MaBan = " & _hoaDonDTO.IMaBan & " and ThanhTien is null"
        Dim dt As New DataTable()
        dt = DataProvider.LoadSQL(sql)
        Return Integer.Parse(dt.Rows(0)("MaHD").ToString())
    End Function

    Public Function LayThongTinHDDAO(ByVal _maHD As Integer) As HoaDonDTO
        Dim sql As String = "Select * from HoaDon where MaHD = " & _maHD
        Dim dt As New DataTable()
        dt = DataProvider.LoadSQL(sql)

        '_iMaHD = New Integer()
        '_iMaBan = New Integer()
        '_dtThoiGianLap = New DateTime()
        '_dThanhTien = New Double()
        '_iMaNV = New Integer()        

        Dim MaBan As Integer = Integer.Parse(dt.Rows(0)("MaBan").ToString())
        Dim ThoiGianLap As DateTime = DateTime.Parse(dt.Rows(0)("ThoiGianLap").ToString())
        Dim ThanhTien As Double
        If (dt.Rows(0)("ThanhTien").ToString() <> "") Then
            ThanhTien = Double.Parse(dt.Rows(0)("ThanhTien").ToString())
        Else
            ThanhTien = 0
        End If

        Dim MaNV As Integer
        If (dt.Rows(0)("MaNV").ToString() <> "") Then
            MaNV = Integer.Parse(dt.Rows(0)("MaNV").ToString())
        Else
            MaNV = Nothing
        End If

        Dim _hdDTO As New HoaDonDTO(_maHD, MaBan, ThoiGianLap, ThanhTien, MaNV)
        Return _hdDTO
    End Function

    Public Function TongTienHDDAO(ByVal _maHD As Integer) As Double
        Dim sql As String = "Select Sum(TongTien) from ChiTiet where MaHD = " & _maHD
        Dim dt As New DataTable()
        dt = DataProvider.LoadSQL(sql)
        If (dt.Rows(0)(0).ToString() <> "") Then
            Return Double.Parse(dt.Rows(0)(0).ToString())
        End If
        Return 0
    End Function

    Public Sub ThanhToanTienHoaDonDAO(ByVal _hdDTO As HoaDonDTO)

        Dim sql As String = "sp_CapNhatTrangThaiBanDaSuDung"
        Dim _listPara As New List(Of SqlParameter)

        Dim MaBan As New SqlParameter("@MaBan", SqlDbType.Int)
        MaBan.Value = _hdDTO.IMaBan
        _listPara.Add(MaBan)

        Dim MaHD As New SqlParameter("@MaHD", SqlDbType.Int)
        MaHD.Value = _hdDTO.IMaHD
        _listPara.Add(MaHD)

        DataProvider.ExcuteQueryProc(sql, _listPara)
    End Sub

End Class
